package com.qianguo.buyer.order;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.qianguo.buyer.R;
import com.qianguo.buyer.base.BaseFragment;
import com.qianguo.buyer.entity.BuyerOrder;
import com.qianguo.buyer.entity.BuyerOrderList;


import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GUXIA
 * @since 2015-10-11
 */
@EFragment(R.layout.fragment_doing_order)
public class DoingOrderFragment extends BaseFragment {

    @ViewById(R.id.swipe_refresh_recycler_view)
    SwipeRefreshRecyclerView mSwipeRefreshRecyclerView;

    private List<BuyerOrder> mOrders = new ArrayList<>();
    private int mCurrentPage = 1;
    private int mTotal;
    private static final int PER_PAGE = 10;

    private DoingOrderAdapter mAdapter = null;

    @Override
    protected void initializeUI() {
        final Context ctx = getApplicationContext();
        mAdapter = new DoingOrderAdapter(ctx);
        mSwipeRefreshRecyclerView.setAdapter(mAdapter);
        mSwipeRefreshRecyclerView.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mOrders.clear();
                loadData(mCurrentPage, PER_PAGE);
            }

            @Override
            public void onLoadMore() {
                loadData(++mCurrentPage, PER_PAGE);
            }
        });

        mSwipeRefreshRecyclerView.setOnItemClickListener(new SwipeRefreshRecyclerView.OnRecyclerItemClickListener() {
            @Override
            public void onClick(int position) {
                BuyerOrderDetailActivity.jumpToOrderDetail(getActivity(), mOrders.get(position).getOrderSN());
            }
        });

        loadData(mCurrentPage, PER_PAGE);
    }

    @Background
    void loadData(int page, int perPage) {
        mApp.getApi().getOrders(mApp.getUser().getToken(), BuyerOrder.TYPE_DOING, page, perPage, new HttpRequestCallback<BuyerOrderList>() {
            @Override
            public void onSuccess(int code, String msg, BuyerOrderList data) {
                mTotal = data.getTotal();
                mOrders.addAll(data.getList());
                refreshUI();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    @UiThread
    void refreshUI() {
        mSwipeRefreshRecyclerView.onCompleted();
        mSwipeRefreshRecyclerView.notifyDataSetChanged();
    }

    private class DoingOrderAdapter extends RecyclerView.Adapter<DoingOrderViewHolder> {

        private Context mContext;
        private DoingOrderAdapter(Context context) {
            mContext = context;
        }

        @Override
        public DoingOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_processing_order, parent, false);
            return new DoingOrderViewHolder(v);
        }

        @Override
        public void onBindViewHolder(DoingOrderViewHolder holder, int position) {
            holder.onBind(mOrders.get(position));
        }

        @Override
        public int getItemCount() {
            return mOrders != null ? mOrders.size() : 0;
        }
    }

    private class DoingOrderViewHolder extends RecyclerView.ViewHolder {

        TextView tvOrderSN;
        TextView tvOrderStatus;
        TextView tvShopName;
        TextView tvDeliveryTime;
        TextView tvOrderTime;
        TextView tvOrderPrice;
        TextView tvPayType;
        ImageView imgShopIcon;

        public DoingOrderViewHolder(final View itemView) {
            super(itemView);
            tvOrderSN = (TextView) itemView.findViewById(R.id.item_doing_order_sn_text_view);
            tvOrderStatus = (TextView) itemView.findViewById(R.id.item_doing_order_status_text_view);
            tvShopName = (TextView) itemView.findViewById(R.id.item_doing_order_shop_name_text_view);
            tvDeliveryTime = (TextView) itemView.findViewById(R.id.item_doing_order_delivery_time_text_view);
            tvOrderTime = (TextView) itemView.findViewById(R.id.item_doing_order_time_text_view);
            tvOrderPrice = (TextView) itemView.findViewById(R.id.item_doing_order_price_text_view);
            tvPayType = (TextView) itemView.findViewById(R.id.item_doing_order_pay_type_text_view);
            imgShopIcon = (ImageView) itemView.findViewById(R.id.item_doing_order_shop_icon_image_view);
        }

        void onBind(BuyerOrder info) {
            tvOrderSN.setText("订单号：" + info.getOrderSN());
            tvOrderStatus.setText(info.getTag());
            tvShopName.setText(info.getStoreName());
            tvDeliveryTime.setText("配送时间：" + info.getDeliveryTime());
            tvOrderTime.setText("下单时间：" + info.getOrderTime());
            tvOrderPrice.setText("订单总价：" + Float.toString(info.getPrice() * 100));
            tvPayType.setText("支付方式：" + info.getPayName());
        }
    }
}
